package com.pg.service.impl;


import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.stereotype.Service;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pg.entity.Dict;
import com.pg.entity.DictItem;
import com.pg.mapper.DictItemMapper;
import com.pg.mapper.DictMapper;
import com.pg.service.DictService;

/**
 * <p>
 * 字典表 服务实现类
 * </p>
 *
 * @Author zhangweijian
 * @since 2018-12-28
 */
@Service
@DS("master")
@Primary
public class DictServiceImpl extends ServiceImpl<DictMapper, Dict> implements DictService {

    @Autowired
    private DictMapper dictMapper;

    @Autowired
    private DictItemMapper dictItemMapper;

    @Override
    public String getDictByCode(String dictCode, String itemValue) {
        LambdaQueryWrapper<Dict> dictLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dictLambdaQueryWrapper.select(Dict::getId);
        dictLambdaQueryWrapper.eq(Dict::getDictCode, dictCode);
        dictLambdaQueryWrapper.last("limit 1");
        Dict dict = dictMapper.selectOne(dictLambdaQueryWrapper);

        LambdaQueryWrapper<DictItem> dictItemLambdaQueryWrapper = new LambdaQueryWrapper<>();
        dictItemLambdaQueryWrapper.eq(DictItem::getDictId, dict.getId());
        dictItemLambdaQueryWrapper.eq(DictItem::getItemValue, itemValue);
        DictItem dictItem1 = dictItemMapper.selectOne(dictItemLambdaQueryWrapper);
        return dictItem1.getItemText();
    }
}
